﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Modles.DTO;
using Modles.Models;

namespace BilbilDAL
{
    public class ConcernDAL
    {

        /// <summary>
        /// 根据用户信息id获取关注人数和被关注人数
        /// </summary>
        /// <param name="UserInfoId"></param>
        /// <returns></returns>
        public ConcernInfo GetConcernList(int UserInfoId)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                ConcernInfo concernInfo = new ConcernInfo();
                concernInfo.Concern = con.Concerns.Where(x => x.UserInfoId == UserInfoId).Count();
                concernInfo.WasNoticed = con.Concerns.Where(x => x.ConcernUserInfoId == UserInfoId).Count();
                return concernInfo;
            }
        }
        /// <summary>
        /// 添加关注
        /// </summary>
        /// <param name="UserInfoId"></param>
        /// <param name="ConcernUserInfoId"></param>
        /// <returns></returns>
        public bool AddConcern(int UserInfoId, int ConcernUserInfoId)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                con.Add(new Concern() { UserInfoId = UserInfoId, ConcernUserInfoId = ConcernUserInfoId });
                return con.SaveChanges() > 0;
            }
        }
        /// <summary>
        /// 取消关注
        /// </summary>
        /// <param name="UserInfoId"></param>
        /// <param name="ConcernUserInfoId"></param>
        /// <returns></returns>
        public bool DeleteConcern(int UserInfoId, int ConcernUserInfoId)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                Concern concern = con.Concerns.FirstOrDefault(x => x.UserInfoId == UserInfoId && x.ConcernUserInfoId == ConcernUserInfoId);
                if(concern == null)
                {
                    return false;
                }
                con.Remove(concern);
                return con.SaveChanges() > 0;
            }
        }
    }
}
